/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ships.core.models;

import ships.core.AbstractDataBuffer;

/**
 *
 * @author fqhuy
 */
public class BandedSampleModel extends ComponentSampleModel{

    public BandedSampleModel(int dataType, int w, int h, int numBands) {
	super(w, h, 1, w,
              BandedSampleModel.createIndicesArray(numBands),
              BandedSampleModel.createOffsetArray(numBands));
    }
    
    private static int[] createOffsetArray(int numBands) {
        int[] bandOffsets = new int[numBands];
        for (int i=0; i < numBands; i++) {
            bandOffsets[i] = 0;
        }
        return bandOffsets;
    }

    private static int[] createIndicesArray(int numBands) {
        int[] bankIndices = new int[numBands];
        for (int i=0; i < numBands; i++) {
            bankIndices[i] = i;
        }
        return bankIndices;
    }

    public BandedSampleModel(int dataType,
			     int w, int h,
			     int scanlineStride,
			     int bankIndices[],
                             int bandOffsets[]) {

	super(w, h, 1,scanlineStride, bankIndices, bandOffsets);
    }

    @Override
    public <T extends Number> T getSample(int x, int y, int b, AbstractDataBuffer data) {
        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
            throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
        }
        T sample = (T)data.getElem(bankIndices[b], y * scanlineStride + x + bandOffsets[b]);
        return sample;
    }
}
